import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class Obstacle {
    public int x;
    public int y;
    public BufferedImage image;
    public BufferedImage stone;
    public BufferedImage cacti;
    public BufferedImage basketball;
    public BufferedImage meteorite;
    protected int speed;

    public static Obstacle getObstacle() {
        Obstacle obstacle;
        // 随机生成障碍
        int randomIndex = new Random().nextInt(5);
        obstacle = switch (randomIndex) {
            case 0 -> new Cacti();
            case 1 -> new Stone();
            case 3 -> new Meteorite();
            default -> new Basketball();
        };
        return obstacle;
    }

    public Obstacle() {
        try {
            stone = ImageIO.read(new File("image/stone.png"));
            cacti = ImageIO.read(new File("image/cacti.png"));
            meteorite = ImageIO.read(new File("image/meteorite.png"));
            basketball = ImageIO.read(new File("image/basketball.png"));
        } catch (IOException var2) {
            var2.printStackTrace();
        }

        x = 800;
        y = 120;
        updateSpeed();
    }

    public void updateSpeed() {
        speed = BackgroundImage.SPEED;
    }

    //移动
    public void move() {
        x -= speed;
    }

    //消除
    public boolean isLive() {
        return x > -image.getWidth();
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, image.getWidth(), image.getHeight());
    }
}

class Basketball extends Obstacle {
    private double angle = 0;

    public Basketball() {
        super();
        image = basketball;
        y = 200 - image.getHeight();
    }

    public double getAngle() {
        angle -= 10;
        return angle;
    }
}

class Meteorite extends Obstacle {
    public Meteorite() {
        super();
        image = meteorite;
        y = 130;
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, image.getWidth() - 10, image.getHeight());
    }
}

class Stone extends Obstacle {
    public Stone() {
        super();
        image = stone;
        y = 210 - image.getHeight();
    }

    public Rectangle getBounds() {
        return new Rectangle(x + 10, y + 15, 60, 45);
    }
}

class Cacti extends Obstacle {
    public Cacti() {
        super();
        image = cacti;
        y = 210 - image.getHeight();
    }

    public Rectangle getBounds() {
        return new Rectangle(x + 7, y, 15, 60);
    }
}